package com.ruoyi.coupon.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitDlxConfig {
/*
* 1、声明正常交换机(normal_exchange)和正常队列(normal_queue)，并绑定
* */
@Bean
public Exchange normalExchange() {
return
ExchangeBuilder.topicExchange("normal_exchange").durable(true).build();
}
@Bean
public Queue normalQueue() {
/*
* 3、正常队列绑定死信交换机
* */
return QueueBuilder.durable("normal_queue")
// 正常队列绑定死信交换机: deadLetterExchange 死信交换机名称deadLetterRoutingKey 死信交换机路由key
.deadLetterExchange("dlx_exchange").deadLetterRoutingKey("dlx.hh")
// 4、设置队列的过期时间
.ttl(20 * 1000)
// 5、设置队列的最大长度限制
.maxLength(10)
.build();
}
@Bean
public Binding bindingNormal() {
    return
            BindingBuilder.bind(normalQueue()).to(normalExchange()).with("normal.#").noargs();
}
    /*
     * 2、声明死信交换机(dlx_exchange)和死信队列(dlx_queue)，并绑定
     * */
    @Bean
    public Exchange dlxExchange() {
        return
                ExchangeBuilder.topicExchange("dlx_exchange").durable(true).build();
    }
    @Bean
    public Queue dlxQueue() {
        return QueueBuilder.durable("dlx_queue").build();
    }
    @Bean
    public Binding bindingDlx() {
        return
                BindingBuilder.bind(dlxQueue()).to(dlxExchange()).with("dlx.#").noargs();
    }
}